package com.qf.data.listener;

import com.qf.common.model.DeviceCmd;
import com.qf.data.service.DeviceCmdService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 接收指令日志保存到mongodb
 * @author 千锋健哥
 */
@Component
@Slf4j
public class DeviceCmdListener {

    @Autowired
    private DeviceCmdService deviceCmdService;

    @RabbitHandler
    @RabbitListener(queues = {"qf.queue.value.cmd"})
    public void messageHandler(Channel channel, DeviceCmd deviceCmd, Message message) {
        try {
            log.info("接收到指令日志:" + deviceCmd);

            //调用service保存指令日志到mongodb
            deviceCmdService.saveDeviceCmd(deviceCmd);

            //手动ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
